﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MLM.AdminCP.Class;
using Telerik.Web.UI;
using MLM.Base;

namespace MLM.AdminCP.Pages
{
    public partial class GroupManager : BasePage
    {
        protected override void OnInit(EventArgs e)
        {
            Permission.CheckIsAdmin();
            base.OnInit(e);
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            Permission.CheckIsAdmin();
            RadGrid1.ItemDataBound += new Telerik.Web.UI.GridItemEventHandler(RadGrid1_ItemDataBound);
            RadGrid1.ItemUpdated += new GridUpdatedEventHandler(RadGrid1_ItemUpdated);
            RadGrid1.ItemInserted += new GridInsertedEventHandler(RadGrid1_ItemInserted);
            RadGrid1.ItemDeleted += new GridDeletedEventHandler(RadGrid1_ItemDeleted);
        }

        void RadGrid1_ItemDeleted(object sender, GridDeletedEventArgs e)
        {
            if (e.Exception != null)
            {
                e.ExceptionHandled = true;
                Message.ShowError("Group " + e.Item["RoleID"].Text + " cannot be deleted. Reason: " + e.Exception.Message);
            }
            else
            {
                Message.ShowSuccess("Group " + e.Item["RoleID"].Text + " deleted!");
            }
        }

        void RadGrid1_ItemInserted(object sender, GridInsertedEventArgs e)
        {
            if (e.Exception != null)
            {
                e.ExceptionHandled = true;
                e.KeepInInsertMode = true;
                Message.ShowError("Group cannot be inserted. Reason: " + e.Exception.Message);
            }
            else
            {
                Message.ShowSuccess("Group inserted!");
            }
        }

        void RadGrid1_ItemUpdated(object sender, GridUpdatedEventArgs e)
        {
            if (e.Exception != null)
            {
                e.KeepInEditMode = true;
                e.ExceptionHandled = true;
                Message.ShowError("Group " + e.Item["RoleID"].Text + " cannot be updated. Reason: " + e.Exception.Message);
            }
            else
            {
                Message.ShowSuccess("Group " + e.Item["RoleID"].Text + " updated.");
            }
        }

        void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
        {
            if (e.Item is GridDataItem)
            {
                GridDataItem item = (GridDataItem)e.Item;
                long role = long.Parse(item["RoleID"].Text);                
                if (role<=4)
                {
                    item["EditCommandColumn"].Text = "";
                    item["DeleteColumn"].Text = "";
                }
                else
                {
                    item["EditCommandColumn"].Visible = true;
                    item["DeleteColumn"].Visible = true;
                }
            }   
        }
    }
}